//
//  WorkflowHistoryViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/30.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "WorkflowHistoryViewController.h"
#import "WorkflowHistoryListCell.h"

NSString *const WorkflowHistoryViewControllerModelIdParamKey = @"WorkflowHistoryViewControllerModelIdParamKey"; 

@interface WorkflowHistoryViewController ()
{
    NSString *_modelId;
    RSWDataOperation *_manager;
    
    NSDate *_lastUpdatetime;
    BOOL _bSearching;
    
    NSMutableArray *_datas;
}
@end

@implementation WorkflowHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    [self checkSession];
    
    [self setBackNavigateItemTitle:@"流程历史"];
    
    [self initVariable];
    [self initUI];
    
    [_manager getWorkflowHistoryModelId:_modelId completion:^(NSInteger nResult, NSArray *list, NSError *error)
     {
         _bRefreshing = NO;
         
         [self requestComplete:nil funcationName:@"getWorkflowHistoryModelId" result:nResult output:list error:error];
     }];
}

- (void)initVariable
{
    _modelId = self.param[WorkflowHistoryViewControllerModelIdParamKey];
    
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
    _datas = [NSMutableArray new];
    
    _lastUpdatetime = [NSDate date];
}

- (void)initUI
{
    [self createTableView:XUITabelViewStyleTypePlain|XUITabelViewStyleTypeRefresh];
    _ctable.separatorExistFollowCell = NO;
    
    _ctable.searchBar.searchText = @"";
}

-(void)viewControllerWillDealloc
{
    [super viewControllerWillDealloc];
    [_manager terminateRequest];
}

-(void)requestComplete:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname result:(NSInteger)nResult output:(id)output error:(NSError *)error
{
    _bRefreshing = NO ;
    
    if([szFuncationname isEqualToString:@"getWorkflowHistoryModelId"])
    {
        [self filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
         {
             switch(requestResult)
             {
                 case YES:
                 {
                     [_datas removeAllObjects];
                     
                     NSArray *ar = output;
                     
                     [_datas addObjectsFromArray:ar];
                     
                     [_ctable reloadData]; 
                     _ctable.separatorStyle = UITableViewCellSelectionStyleNone;
                     
                     break;
                 }
                 default:
                 {
                     [UIAlertView showErrorMsg:@"数据请求失败"];
                     break;
                 }
             }
         }];
    }
}

#pragma mark --
#pragma mark -- tableView delegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [WorkflowHistoryListCell caleHeight:tableView.width];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datas.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkflowHistoryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkflowHistoryListCell"];
    if(cell == nil)
    {
        cell = [[WorkflowHistoryListCell alloc]initWithReuseIdentifier:@"WorkflowHistoryListCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    WorkflowHistoryInfo *bi = _datas[indexPath.row];
    
    cell.szTag = [bi.historyId toNSString];
    
    cell.changedTime = bi.changedTime;
    cell.changedUserName = bi.changedUserName;
    cell.activityName = bi.activityName;
    
    return cell;
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    [UIViewController closeKeyBoard];
}

-(id)contentLastRefreshUpdateTimeOfTableView:(XUITableView *)tableview
{
    return _lastUpdatetime;
}

-(void)tableView:(XUITableView *)tableview refreshTableContent:(void (^)())asyncEndRefreshing
{
    if(_bRefreshing == YES)
        return;
    
    _bRefreshing = YES;
    [_manager stopRequest];
    
    [_manager getWorkflowHistoryModelId:_modelId completion:^(NSInteger nResult, NSArray *list, NSError *error)
     {
         if(nResult == YES)
             _lastUpdatetime = [NSDate date];
         
         _bRefreshing = NO;
         
         [self requestComplete:nil funcationName:@"getWorkflowHistoryModelId" result:nResult output:list error:error];
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
